package com.schoolCommunity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.schoolCommunity.entity.Commodity;
import com.schoolCommunity.entity.CommodityTransaction;
import com.schoolCommunity.entity.Information;
import com.schoolCommunity.mapper.CommodityTransactionMapper;
import com.schoolCommunity.service.CommodityTransactionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author YTH
 * @since 2020-10-10
 */
@Service
public class CommodityTransactionServiceImpl extends ServiceImpl<CommodityTransactionMapper, CommodityTransaction> implements CommodityTransactionService {

    @Resource
    private CommodityTransactionMapper commodityTransactionMapper;

    //添加购买记录
    @Override
    public int InsertCommodityTransaction(long BuyMemberId, Commodity commodity) {
        Information information = new Information();
        CommodityTransaction commodityTransaction = new CommodityTransaction();
        commodityTransaction.setBuyMemberId(BuyMemberId).setCommodityId(commodity.getId()).setMemberId(commodity.getMemberId())
                .setPrice(commodity.getPrice()).setTime(new Date());
        int insert = commodityTransactionMapper.insert(commodityTransaction);
        return insert;
    }

    @Override
    public Information GetCommodityTransactionById(long memberId) {
        Information information = new Information();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("buy_member_id", memberId);
        List list = commodityTransactionMapper.selectList(queryWrapper);
        if (list.isEmpty()) {
            information.setCode(404).setMessage("没有找到数据");
        } else {
            information.setCode(200).setMessage("数据查找成功").setData(list);
        }
        return information;
    }
}
